import { Component, OnInit, Input, Output, DoCheck, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-notification',
  templateUrl: 'notification.component.html',
  styleUrls: ['notification.component.scss'],
  providers: [

  ]
})

export class NotificationComponent implements OnInit, DoCheck {

  @Input() title: string;
  @Input() type: string;             // 类型 'success':成功 'error':错误 'warning':警告 'info':提示
  @Input() message: string;          // 提示信息文字
  @Input() duration = 5000;     // 显示时间，为0时不关闭
  @Input() isShow = true;
  _isShow: boolean;
  _title: string;

  @Output() promptIsShow = new EventEmitter();

  constructor() { }

  ngOnInit() {
    this.setTitle();
  }

  // 设置头
  setTitle(){
    if (!this.title) {
      switch(this.type){
        case 'success':
          this._title = '成功';
          break;
        case 'warning':
          this._title = '警告';
          break;
        case 'error':
          this._title = '错误';
          break;
        case 'info':
          this._title = '提示';
          break;
      }
    } else {
      this._title = this.title;
    }
  }

  ngDoCheck() {
    if (this.isShow === true) {
      this._isShow = this.isShow;
      this.setTitle();
      this.delayed();
    }
  }

  // 设置自动关闭隐藏的时间
  delayed(){
    if(this.duration != 0){
      setTimeout(
        () => {
          this._isShow = false;
          this.promptIsShow.emit(this._isShow);
        }
      ,this.duration)
    }
  }

  // 关闭提示框
  closePrompt() {
    this._isShow = false;
    this.promptIsShow.emit(this._isShow);
  }
}
